package com.bjsxt.users.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.users.dao.IAdminsDao;
import com.bjsxt.users.pojo.AAdmins;
import com.bjsxt.users.service.IUsersService;

@Service("usersService")
public class UsersServiceImpl implements IUsersService {
	@Resource
	private IAdminsDao adminsDao;

	@Override
	public JSONObject insertOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject();

		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("email", admins.getEmail());
		if (this.adminsDao.findOne(condMap) == null) {

			int res = adminsDao.insert(admins);
			if (res > 0) {
				resultJSON.put("code", "0");
				resultJSON.put("info", "添加成功");
				JSONObject dataJSON = new JSONObject();
				dataJSON.put("id", admins.getId());
				resultJSON.put("data", dataJSON);
			} else {
				resultJSON.put("code", "1");
				resultJSON.put("info", "添加失败");
			}

		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败邮箱已存在");
		}
	
		return resultJSON;
	}

	// 查询一条记录
	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap) {

		return this.adminsDao.findOne(condMap);
	}

	// 更新一条记录
	@Override
	public JSONObject updateOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject();
		int res = adminsDao.update(admins);
		if (res > 0) {
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId());
			resultJSON.put("data", dataJSON);

		} else {
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}

		return resultJSON;
	}

	// 删除一条记录
	@Override
	public JSONObject deletetOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject();
		int res = adminsDao.delete(admins);
		if (res > 0) {
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");

		} else {
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}

	// 查询多条记录
	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		List<AAdmins> adminsList = new ArrayList<AAdmins>();
		// 处理关键字
		if (condMap != null) {
			if (condMap.get("keyword") != null) {
				condMap.put("keyword", "%" + condMap.get("keyword") + "%");
			}
		}
		/*
		 * pageCount == true:拿总记录数: pageCount == false:分页数据: pageCount
		 * 为null不分页查询
		 */
		if (pageInfoUtil != null) {
			// 查询总记录数
			condMap.put("pageCount", "true");
			adminsList = this.adminsDao.findCondList(condMap);
			// 取得总记录数（总记录数赋值给了ID）
			pageInfoUtil.setTotalRecord(adminsList.get(0).getId());
			// 设置分页数据
			condMap.put("pageCount", "false");
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord() - 1);
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			adminsList = this.adminsDao.findCondList(condMap);

		} else {
			// 不分页查询
			adminsList = this.adminsDao.findCondList(condMap);
		}
		return adminsList;
	}

}
